package dang.interceptor;

import java.sql.Connection;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

import dang.common.DBUtil;

public class TransactionInterceptor extends MethodFilterInterceptor {

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		Connection con = DBUtil.openInThread();
		con.setAutoCommit(false);
		try{
			String resultCode = invocation.invoke();//执行Action业务方法
			con.commit();
			return resultCode;
		}catch(Exception e){
			//TODO 记录异常信息,写入日志文件
			e.printStackTrace();
			con.rollback();
			return "error";
		}finally{
			DBUtil.closeInThread();
		}
	}

}
